import * as vscode from "vscode";

export async function loadMark(markFilePath: vscode.Uri) {
  try {
    const markByte = await vscode.workspace.fs.readFile(markFilePath);
    const markFile = new TextDecoder("utf-8").decode(markByte);
    return markFile;
  } catch (err) {
    console.error(err);
    return "";
  }
}

export function parseMark(input: string): {
  catalog: { [key: string]: string };
  dictionary: { [key: string]: string };
} {
  const lines = input.split("\n");
  let head1: string = "",
    head2: string = "";

  const dictionary: { [key: string]: string } = {};
  const catalog: { [key: string]: string } = {};
  for (const line of lines) {
    // 获取键
    const cap1 = line.indexOf("#");
    if (cap1 !== -1) {
      const temp = line.substring(cap1 + 1).trim();
      const cap2 = temp.indexOf("#");
      if (cap2 === -1) {
        head1 = temp;
      } else {
        head2 = temp.substring(cap2 + 2);
        dictionary[head2] = "";
        catalog[head2] = head1;
      }
      // 获取值
    } else {
      const value = line.trim();
      dictionary[head2] += value + "\n\n";
    }
  }

  return {
    catalog: catalog,
    dictionary: dictionary,
  };
}
